package com.hp.utils.excel.strategy.impl;


import com.hp.enums.ExcelCellTypeEnum;
import com.hp.utils.excel.ExcelConstraintResolve;
import com.hp.utils.excel.strategy.ConstraintTypeStrategy;
import jakarta.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * @author hp
 */
@Component
public class PositiveIntegerConstraintTypeStrategy extends BasicConstraintStrategyFactory implements ConstraintTypeStrategy {
    @Override
    public void process(ExcelConstraintResolve resolve, Map<String, ExcelConstraintResolve> tempKeyMap, DataValidationHelper helper, Sheet hiddenSheet, Sheet sheet) {
        String minValue = StringUtils.isNotBlank(resolve.getMinValue()) ? resolve.getMinValue() : "0";
        // 约束
        DataValidationConstraint constraint = helper.createIntegerConstraint(DataValidationConstraint.OperatorType.GREATER_THAN, minValue, null);
        // 设置单元格验证
        this.setCellValidation(resolve, helper, constraint, sheet, true, true);

    }

    @PostConstruct
    public void init() {
        CELL_TYPE_STRATEGY_MAP.put(ExcelCellTypeEnum.POSITIVE_INTEGER.getType(), this);
    }
}